package com.yx.gsyd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yx.gsyd.comm.DictionaryEnum;
import com.yx.gsyd.comm.ResultEnum;
import com.yx.gsyd.entity.SysMenu;
import com.yx.gsyd.entity.req.SysMenuReq;
import com.yx.gsyd.exception.MyException;
import com.yx.gsyd.mapper.SysMenuMapper;
import com.yx.gsyd.service.ISysMenuService;
import com.yx.gsyd.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-06-17
 */

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    /**
     * 显示所有菜单列表
     * @return
     */
    @Override
    public List<SysMenu> getListAll(String menuName) {
        QueryWrapper<SysMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotEmpty(menuName),SysMenu::getMenuName,menuName);
        queryWrapper.eq("is_del", DictionaryEnum.IS_DEL_N.getCode());
        queryWrapper.orderByAsc("menu_Num");
        List<SysMenu> menuList=this.list(queryWrapper);
        return menuList;
    }

    /**
     * 添加菜单
     * @param hjMenuReq
     * @return
     */
    @Transactional
    @Override
    public ResultView addMenu(SysMenuReq hjMenuReq) {
        //如果是菜单
        if(hjMenuReq.getMenuType()==DictionaryEnum.MENU_TYPE_TWO.getCode()){
            if(StringUtils.isEmpty(hjMenuReq.getParentId())){
                throw new MyException(ResultEnum.CODE_2,"添加权限为菜单类型时候，父级菜单不能为空");
            }
        }
        //如果是按钮
        if(hjMenuReq.getMenuType()==DictionaryEnum.MENU_TYPE_THREE.getCode()){
            if(StringUtils.isEmpty(hjMenuReq.getParentId())){
                throw new MyException(ResultEnum.CODE_2,"添加权限为按钮类型时候，父级菜单不能为空");
            }
            if(StringUtils.isEmpty(hjMenuReq.getMenuPerm())){
                throw new MyException(ResultEnum.CODE_2,"添加权限为按钮类型时候，权限字段不能为空");
            }
        }
        SysMenu hjMenu=new SysMenu();
        BeanUtils.copyProperties(hjMenuReq,hjMenu);
        hjMenu.setMenuId(IdentityUtil.identityId("MU"));
        hjMenu.setCtime(new Date());
        hjMenu.setIsDel(DictionaryEnum.IS_DEL_N.getCode());
        boolean count=this.save(hjMenu);
        if(!count){
            throw new MyException(ResultEnum.CODE_2,"添加权限出现异常");
        }
        return ResultView.ok();
    }


    /**
     * 修改菜单
     * @param hjMenuReq
     * @return
     */
    @Transactional
    @Override
    public ResultView updateMenu(SysMenuReq hjMenuReq) {
        SysMenu hjMenu=new SysMenu();
        BeanUtils.copyProperties(hjMenuReq,hjMenu);
        hjMenu.setUtime(new Date());
        boolean count=this.updateById(hjMenu);
        if(!count){
            throw new MyException(ResultEnum.CODE_2,"修改菜单出现异常");
        }
        return ResultView.ok();
    }

    /**
     * 菜单删除
     * @param menuId
     * @return
     */
    @Transactional
    @Override
    public ResultView deleteMenu(String menuId) {
        //检查当前菜单是否还有子菜单
        QueryWrapper<SysMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",menuId);
        queryWrapper.eq("is_del",DictionaryEnum.IS_DEL_N.getCode());
        List<SysMenu> menuList=this.list(queryWrapper);
        if(menuList!=null&&menuList.size()>=1){
            throw new MyException(ResultEnum.CODE_2,"菜单删除失败，该菜单下面还存在子菜单");
        }
        SysMenu hjMenu=new SysMenu();
        hjMenu.setMenuId(menuId);
        hjMenu.setUtime(new Date());
        hjMenu.setIsDel(DictionaryEnum.IS_DEL_Y.getCode());
        boolean isOk=this.updateById(hjMenu);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"删除菜单出现异常");
        }
        return ResultView.ok();
    }

    /**
     * 根据菜单ID获取当前数据
     * @param menuId
     * @return
     */
    @Override
    public ResultView getMenuId(String menuId) {
        SysMenu hjMenu=this.getById(menuId);
        return ResultView.ok(hjMenu);
    }

    /**
     * 获取当前用户树形菜单
     * @param managerId
     * @return
     */
    @Override
    public ResultView getMenuTree(String managerId) {
        List<Tree<SysMenu>> trees = new ArrayList<Tree<SysMenu>>();
        //获取当前用户树形菜单集合
        List<SysMenu> menuList=this.baseMapper.getMenuTree(managerId);
        for (SysMenu menu : menuList) {
            Tree<SysMenu> tree = new Tree<SysMenu>();
            tree.setId(menu.getMenuId());
            tree.setParentId(menu.getParentId());
            if(menu.getMenuType()==DictionaryEnum.MENU_TYPE_ONE.getCode()){
                tree.setPath("/"+menu.getMenuUrl());
                tree.setAlwaysShow(true);
                tree.setRedirect("noRedirect");
            }else{
                tree.setPath(menu.getMenuUrl());
            }
            if(StringUtils.isNotEmpty(menu.getMenuUrl())){
                tree.setName(LvsUtil.captureName(menu.getMenuUrl()));
            }
            tree.setHidden(false);
            tree.setComponent(LvsUtil.isNull(menu.getParentId()) ? "Layout" : menu.getMenuComponent());
            Map<String, Object> attributes = new HashMap<>(16);
            attributes.put("title", menu.getMenuName());
            attributes.put("icon", menu.getMenuIcon());
            attributes.put("noCache", true);
            tree.setMeta(attributes);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        List<Tree<SysMenu>> list = BuildTree.buildList(trees, "");
        return ResultView.ok(list);
    }


}
